package com.bzframework.pro.authorize.token.parser;

import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Optional;

import static com.bzframework.pro.authorize.constants.TokenNameEnum.HEADER_TOKEN_NAME;
import static com.bzframework.pro.authorize.constants.TokenNameEnum.PARAMETER_TOKEN_NAME;

/**
 * @Author bingzhong
 * @Date 2024/11/24 14:15
 * @Description
 */
public class DefaultTokenParser implements TokenParser {

    @Override
    public Mono<ParsedToken> parseToken(ServerWebExchange exchange) {

        String token = Optional.ofNullable(exchange.getRequest()
                                                   .getHeaders()
                                                   .getFirst(HEADER_TOKEN_NAME.getText()))
                               .orElseGet(() -> exchange.getRequest().getQueryParams().getFirst(PARAMETER_TOKEN_NAME.name()));
        if (token == null) {
            return Mono.empty();
        }
        return Mono.just(ParsedToken.of(token));
    }
}
